import { message } from 'antd';

// import { history } from 'umi'
// import cookie from 'js-cookie';
// import { useRequest } from '@/hooks';
// import { fakeAccountLogin } from '@/services/api';
import { fakeAccountLogin } from '@/pages/RecruitEmail/service/interview';
import { pushRoute } from '../utils';

const useOldLogin = location => {
    const { query } = location;

    const handleLogin = async params => {
        try {
            const result = await fakeAccountLogin({
                loginName: params.talentEmail,
                password: params.password,
                organizeType: 1
            });
            const { code, msg, data: resData } = result || {};
            if (code === 10000) {
                localStorage.setItem('candidateUserCode', resData?.userCode);

                localStorage.setItem('loginInfo', JSON.stringify(resData));
                const { redirect, ...rest } = query;
                let outQuery = {};
                const cId = params?.id;
                if (!cId) {
                    outQuery = rest;
                } else {
                    outQuery = { ...rest, id: params?.id };
                }
                if (redirect) {
                    pushRoute({
                        pathname: redirect,
                        query: outQuery
                    });
                } else {
                    // history.goBack();
                    // 跳转到首页
                    pushRoute({
                        pathname: '/recruitment',
                        query: outQuery
                    });
                }
            } else if (msg) {
                message.error(msg);
            }
        } catch (error) {
            console.log('onError', error);
        }
    };

    // const { loading, run: handleLogin } = useRequest(
    //     params => {
    //         console.log('params', params);
    //         if (params?.id) {
    //             setCurrId(params?.id);
    //         }
    //         fakeAccountLogin({
    //             loginName: params.talentEmail,
    //             password: params.password,
    //             organizeType: 1
    //         });
    //     },
    //     {
    //         manual: true,
    //         initialData: null,
    //         onError: err => {
    //             console.log('onError', err);
    //         },
    //         onSuccess: res => {
    //             console.log('onSuccess', res);
    //             // const { data: resDataTemp, response } = res || {};
    //             const { code, msg, data: resData } = res || {};
    //             if (code === 10000) {
    //                 localStorage.setItem('candidateUserCode', resData?.userCode);

    //                 localStorage.setItem('loginInfo', JSON.stringify(resData));
    //                 const { redirect, ...rest } = query;
    //                 if (redirect) {
    //                     pushRoute({
    //                         pathname: redirect,
    //                         query: {
    //                             id: currId,
    //                             ...rest
    //                         }
    //                     });
    //                 } else {
    //                     // history.goBack();
    //                     // 跳转到首页
    //                     pushRoute({
    //                         pathname: '/recruitment',
    //                         query: {
    //                             id: currId,
    //                             ...rest
    //                         }
    //                     });
    //                 }
    //             } else if (msg) {
    //                 message.error(msg);
    //             }
    //         }
    //     }
    // );

    return {
        // loading,
        handleLogin
    };
};

export default useOldLogin;
